package com.white.algebra.controller;

import com.white.algebra.service.ClassifyService;
import com.white.algebra.service.impl.ClassifyServiceImpl;
import com.white.algebra.utils.StringUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.List;

public class CalculateController extends BaseServlet{
    private HttpServletRequest req;

    protected void calculate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String textA=req.getParameter("textA");
        String textB=req.getParameter("textB");
        String selectOption=req.getParameter("selectOption");
        ClassifyService classifyService=new ClassifyServiceImpl();
        String end = classifyService.classify(textA,textB,selectOption);
        req.setAttribute("textA",textA);
        req.setAttribute("textB",textB);
        req.setAttribute("end",end);
        req.getRequestDispatcher("/pages/main.jsp").forward(req,resp);
    }
    protected void fileCalculate( HttpServletRequest req,  HttpServletResponse resp) throws ServletException, IOException, FileUploadException {
        String selectOption = null;
        DiskFileItemFactory factory = new DiskFileItemFactory();
        String path = req.getRealPath("/upload");
        //缓存设置
        factory.setRepository(new File(path));
        factory.setSizeThreshold(1024*1024) ;
        ServletFileUpload upload = new ServletFileUpload(factory);
        List<FileItem> list = upload.parseRequest(req);
        String textA="";
        String textB="";
        for(int i=0;i<list.size();i++){
            FileItem item= list.get(i);
            if(item.isFormField()){
                selectOption=item.getString("utf-8");
            }else {
                InputStream in = item.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                String line;
                if (i == 0) {
                    while ((line = bufferedReader.readLine()) != null) {
                        textA = textA + line + "\r\n";
                    }
                } else {
                    while ((line = bufferedReader.readLine()) != null) {
                        textB = textB + line + "\r\n";
                    }
                }
            }

        }
        textA=textA.replace(',',' ');
        textB=textB.replace(',',' ');
        final String finalTextA = textA;
        final String finalTextB = textB;
        String finalSelectOption = selectOption;
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
                try {
                    ClassifyService classifyService=new ClassifyServiceImpl();
                    String value=classifyService.classify(finalTextA, finalTextB, finalSelectOption);
                    System.out.println("value="+value);
                    PrintWriter pw=setDownload(req,resp);
                    value=value.replace(" ", ",").replace("\r\n","\n");
                    pw.print(value);
//                    req.setAttribute("value",value);
//                    req.getRequestDispatcher("/pages/file-calculate.jsp").forward(req,resp);
                } catch (IOException e) {
                    e.printStackTrace();
                }
//            }
//        }).start();
    }
    public PrintWriter setDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        // 设置csv文件下载头信息
//        response.setContentType("text/csv");
//        response.setHeader("Content-Disposition", "attachment; filename=\"userDirectory.csv\"");
//        response.setCharacterEncoding("utf-8");

        String fileName = "download.csv";
//        response.setCharacterEncoding("gbk");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/csv");
        //response.setContentType(request.getServletContext().getMimeType(fileName));
        //设置以附件打开
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        // 2、导出csv文件
        PrintWriter pw = response.getWriter();

        return pw;
    }

}
